<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    div {
      width: 200px;
      height: 50px;
      display: flex;
      margin: 50px;
    }

    div > input {
      width: 100px;
      text-align: center;
      font-size: 20px;
    }

    div > button {
      width: 50px;
      height: 50px;
      font-size: 22px;
    }
  </style>
</head>
<body>

  <div>
    <button class="sub">-</button>
    <input  type="text" value="1">
    <button class="add">+</button>
  </div>

  <div>
    <button class="sub2">-</button>
    <input class="second" type="text" value="1">
    <button class="add2">+</button>
  </div>

  <script>
        function outer(){
            let count = 1
            return {
                getCount(){return count},
                setCount(val){count = val}
            }
        }

        const sub = document.querySelector('.sub');
        const add = document.querySelector('.add');
        const firstinput = document.querySelector('input');

        const box = outer();
        sub.onclick = function(){
            
            let num = box.getCount();
            if(num<=0){
                firstinput.value = 0;
                return;
            }
            box.setCount(num-1);
            
            firstinput.value = num;
        }

        add.onclick = function(){
            let num = box.getCount();
            box.setCount(num+1);
            firstinput.value = num;
        }



        const sub2 = document.querySelector('.sub2');
        const add2 = document.querySelector('.add2');
        const secondinput = document.querySelector('.second');


        const box1 = outer();
        sub2.onclick = function(){
            
            let num = box.getCount();
            if(num<=0){
                secondinput.value = 0;
                return;
            }
            box.setCount(num-1);
            
            secondinput.value = num;
        }

        add2.onclick = function(){
            let num = box.getCount();
            box.setCount(num+1);
            secondinput.value = num;
        }


  </script>

  </body>
  </html>